package com.ctshk.rpc.order.goods.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.order.goods.dto.OrderCommodityCollectionDTO;
import com.ctshk.rpc.order.goods.entity.OrderCommodityCollection;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 商品订单收款明细 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-19
 */
public interface OrderCommodityCollectionMapper extends BaseMapper<OrderCommodityCollection> {

    @Select({
            "<script>",
            "SELECT ",
            "t.id,t.order_id,t.collection_number,t.standard_amount,t.collection_amount," +
                    "t.collection_way_id,t.collection_way,t.payer,t.saler_id,t.saler_name,t.gmt_create," +
                    "t.status,t.reject_info,t.currency_id,t.exchange_rate,t.currency ",
            "FROM `ctshk-goods-order`.t_order_commodity_collection t ",
            "WHERE t.is_deleted = 0 AND t.order_id = #{orderId} ",
            "ORDER BY t.gmt_create DESC",
            "</script>"
    })
    List<OrderCommodityCollectionDTO> findOrderCommodityCollectionByOrderId(@Param("orderId") Long orderId);

    /**
     * 新增订单明细
     * @param orderCommodityCollection
     */
    @Insert({
            "INSERT INTO `ctshk-goods-order`.t_order_commodity_collection " +
                    "( id, collection_number, order_id, is_pos, collection_amount, currency_id, exchange_rate, currency, " +
                    "is_unionpay_discount, unionpay_discount_id, collection_way_id, collection_way, payer, " +
                    "saler_id, saler_name, create_id, modified_id, is_deleted, gmt_create, gmt_modified ) " +
                    "VALUES ( #{id}, #{collectionNumber}, #{orderId}, #{isPos}, #{collectionAmount}, #{currencyId}, #{exchangeRate}, #{currency}, #{isUnionpayDiscount}, " +
                    "#{unionpayDiscountId}, #{collectionWayId}, #{collectionWay}, #{payer}, " +
                    "#{salerId}, #{salerName}, #{createId}, #{modifiedId}, #{isDeleted}, #{gmtCreate}, #{gmtModified} )"
    })
    void insertOrderCommodityCollection(OrderCommodityCollection orderCommodityCollection);

    /**
     * 根据收款单号查询商品订单收款明细
     * @param collectionNumber
     * @return
     */
    @Select({
            "SELECT * FROM `ctshk-goods-order`.t_order_commodity_collection WHERE collection_number = #{collectionNumber} AND is_deleted = 0"
    })
    OrderCommodityCollection findOrderCommodityCollectionByCollectionNumber(@Param("collectionNumber") String collectionNumber);

    /**
     * 更新驳回收款记录
     * @param orderCommodityCollection
     */
    @Update({
            "<script>" +
            "UPDATE `ctshk-goods-order`.`t_order_commodity_collection` " +
                    " <trim prefix=\"set\" suffixOverrides=\",\"> " +
                    "<if test=\"collectionNumber != null\">`collection_number` = #{collectionNumber}, </if>" +
                    "<if test=\"orderId != null\">`order_id` = #{orderId}, </if>" +
                    "<if test=\"isPos != null\">`is_pos` = #{isPos}, </if>" +
                    "<if test=\"standardAmount != null\">`standard_amount` = #{standardAmount}, </if>" +
                    "<if test=\"collectionAmount != null\">`collection_amount` = #{collectionAmount}, </if>" +
                    "<if test=\"currencyId != null\">`currency_id` = #{currencyId}, </if>" +
                    "<if test=\"exchangeRate != null\">`exchange_rate` = #{exchangeRate}, </if>" +
                    "<if test=\"currency != null\">`currency` = #{currency}, </if>" +
                    "<if test=\"isUnionpayDiscount != null\">`is_unionpay_discount` = #{isUnionpayDiscount}, </if>" +
                    "<if test=\"unionpayDiscountId != null\">`unionpay_discount_id` = #{unionpayDiscountId}, </if>" +
                    "<if test=\"collectionWayId != null\">`collection_way_id` = #{collectionWayId}, </if>" +
                    "<if test=\"collectionWay != null\">`collection_way` = #{collectionWay}, </if>" +
                    "<if test=\"payer != null\">`payer` = #{payer}, </if>" +
                    "<if test=\"remark != null\">`remark` = #{remark}, </if>" +
                    "<if test=\"collectionCertificateJson != null\">`collection_certificate_json` = #{collectionCertificateJson}, </if>" +
                    "<if test=\"salerId != null\">`saler_id` = #{salerId}, </if>" +
                    "<if test=\"salerName != null\">`saler_name` = #{salerName}, </if>" +
                    "<if test=\"status != null\">`status` = #{status}, </if>" +
                    "<if test=\"rejectInfo != null\">`reject_info` = #{rejectInfo}, </if>" +
                    "<if test=\"gmtCreate != null\">`gmt_create` = #{gmtCreate}, </if>" +
                    "<if test=\"createId != null\">`create_id` = #{createId}, </if>" +
                    "<if test=\"gmtModified != null\">`gmt_modified` = #{gmtModified}, </if>" +
                    "<if test=\"modifiedId != null\">`modified_id` = #{modifiedId}, </if>" +
                    "<if test=\"isDeleted != null\">`is_deleted` = #{isDeleted}, </if>" +
                    "<if test=\"receiptUp != null\">`receipt_up` = #{receiptUp}, </if>" +
                    "</trim>" +
                    "WHERE `id` = #{id}"+
            "</script>"
    })
    void updateCommodityCollectionById(OrderCommodityCollection orderCommodityCollection);


    /**
     * 根据id查询商品订单收款明细
     * @param id
     * @return
     */
    @Select({
            "SELECT\n" +
                    "\t`id`,\n" +
                    "\t`collection_number`,\n" +
                    "\t`order_id`,\n" +
                    "\t`is_pos`,\n" +
                    "\t`standard_amount`,\n" +
                    "\t`collection_amount`,\n" +
                    "\t`currency_id`,\n" +
                    "\t`exchange_rate`,\n" +
                    "\t`currency`,\n" +
                    "\t`is_unionpay_discount`,\n" +
                    "\t`unionpay_discount_id`,\n" +
                    "\t`collection_way_id`,\n" +
                    "\t`collection_way`,\n" +
                    "\t`payer`,\n" +
                    "\t`remark`,\n" +
                    "\t`collection_certificate_json`,\n" +
                    "\t`saler_id`,\n" +
                    "\t`saler_name`,\n" +
                    "\t`status`,\n" +
                    "\t`reject_info`,\n" +
                    "\t`gmt_create`,\n" +
                    "\t`create_id`,\n" +
                    "\t`gmt_modified`,\n" +
                    "\t`modified_id`,\n" +
                    "\t`is_deleted`,\n" +
                    "\t`receipt_up` \n" +
                    "FROM  `ctshk-goods-order`.`t_order_commodity_collection` \n" +
                    "WHERE id = #{id}"
    })
    OrderCommodityCollection selectCollectionById(@Param("id") Long id);

    /**
     * 订单id查询商品收款记录
     * @param orderId
     * @return
     */
    @Select({
            "SELECT * FROM `ctshk-goods-order`.t_order_commodity_collection WHERE order_id = #{orderId}"
    })
    List<OrderCommodityCollection> selectCollectionByOrderId(@Param("orderId") Long orderId);
}
